<script setup>
import { ref, onMounted, computed } from 'vue'
import { RouterLink } from 'vue-router'
import { useProductsStore } from '../stores/products'

const productsStore = useProductsStore()
const currentBannerIndex = ref(0)

// 获取轮播图数据
const banners = computed(() => productsStore.banners)

// 获取分类数据
const categories = computed(() => productsStore.categories)

// 获取热销商品（前4个）
const hotProducts = computed(() => {
  return [...productsStore.products]
    .sort((a, b) => b.sales - a.sales)
    .slice(0, 4)
})

// 自动轮播
let bannerInterval = null

const startBannerRotation = () => {
  bannerInterval = setInterval(() => {
    currentBannerIndex.value = (currentBannerIndex.value + 1) % banners.value.length
  }, 3000)
}

const stopBannerRotation = () => {
  if (bannerInterval) {
    clearInterval(bannerInterval)
    bannerInterval = null
  }
}

const goToBanner = (index) => {
  currentBannerIndex.value = index
}

// 加载数据
onMounted(() => {
  productsStore.loadProducts()
  startBannerRotation()
})

// 清理定时器
const cleanup = () => {
  stopBannerRotation()
}

// 组件卸载时清理
import { onUnmounted } from 'vue'
onUnmounted(() => {
  cleanup()
})
</script>

<template>
  <main class="home-container">
    <!-- 轮播Banner -->
    <section class="banner-section">
      <div 
        class="banner-carousel"
        @mouseenter="stopBannerRotation"
        @mouseleave="startBannerRotation"
      >
        <div class="banner-wrapper">
          <div 
            v-for="(banner, index) in banners" 
            :key="banner.id"
            class="banner-item"
            :class="{ active: index === currentBannerIndex }"
          >
            <RouterLink :to="banner.link">
              <img :src="banner.image" :alt="banner.title" class="banner-image" />
              <div class="banner-content">
                <h2 class="banner-title">{{ banner.title }}</h2>
                <p class="banner-description">{{ banner.description }}</p>
              </div>
            </RouterLink>
          </div>
        </div>
        <!-- Banner指示器 -->
        <div class="banner-indicators">
          <button
            v-for="(banner, index) in banners"
            :key="banner.id"
            class="indicator"
            :class="{ active: index === currentBannerIndex }"
            @click="goToBanner(index)"
          ></button>
        </div>
      </div>
    </section>

    <!-- 分类导航 -->
    <section class="categories-section">
      <div class="container">
        <h2 class="section-title">商品分类</h2>
        <div class="categories-grid">
          <RouterLink
            v-for="category in categories"
            :key="category.id"
            :to="{ name: 'products', query: { category: category.id } }"
            class="category-item"
          >
            <span class="category-icon">{{ category.icon }}</span>
            <span class="category-name">{{ category.name }}</span>
          </RouterLink>
        </div>
      </div>
    </section>

    <!-- 热销商品 -->
    <section class="hot-products-section">
      <div class="container">
        <div class="section-header">
          <h2 class="section-title">热销商品</h2>
          <RouterLink to="/products" class="view-more">查看更多 →</RouterLink>
        </div>
        <div class="products-grid">
          <div v-for="product in hotProducts" :key="product.id" class="product-card">
            <RouterLink :to="`/products/${product.id}`" class="product-link">
              <div class="product-image-wrapper">
                <img :src="product.image" :alt="product.name" class="product-image" />
                <span v-if="product.discount" class="discount-badge">{{ product.discount }}</span>
              </div>
              <div class="product-info">
                <h3 class="product-name">{{ product.name }}</h3>
                <div class="product-rating">
                  <span class="rating-stars">⭐</span>
                  <span class="rating-value">{{ product.rating }}</span>
                  <span class="rating-count">({{ product.reviewCount }})</span>
                </div>
                <div class="product-price">
                  <span class="current-price">¥{{ product.price }}</span>
                  <span class="original-price">¥{{ product.originalPrice }}</span>
                </div>
                <div class="product-sales">已售 {{ product.sales }}</div>
              </div>
            </RouterLink>
          </div>
        </div>
      </div>
    </section>
  </main>
</template>

<style scoped>
.home-container {
  min-height: calc(100vh - 130px);
  padding-bottom: 0;
  background-color: #f8f9fa;
}

/* Banner样式 */
.banner-section {
  margin-bottom: 0;
}

.banner-carousel {
  position: relative;
  width: 100%;
  height: 500px;
  overflow: hidden;
  background-color: #ffffff;
}

.banner-wrapper {
  position: relative;
  height: 100%;
}

.banner-item {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  transition: opacity 0.5s ease;
}

.banner-item.active {
  opacity: 1;
  z-index: 1;
}

.banner-link {
  display: block;
  width: 100%;
  height: 100%;
  position: relative;
}

.banner-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.banner-content {
  position: absolute;
  top: 50%;
  left: 10%;
  transform: translateY(-50%);
  color: #212529;
  z-index: 2;
}

.banner-title {
  font-size: 48px;
  font-weight: 600;
  margin-bottom: 16px;
  line-height: 1.2;
}

.banner-description {
  font-size: 20px;
  margin-bottom: 24px;
  color: #6c757d;
}

.banner-indicators {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
  z-index: 3;
}

.indicator {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: none;
  background-color: rgba(108, 117, 125, 0.3);
  cursor: pointer;
  transition: all 0.3s ease;
}

.indicator.active {
  background-color: #6c757d;
  width: 30px;
  border-radius: 6px;
}

/* 分类样式 */
.categories-section {
  margin: 60px 0;
  padding: 40px 0;
  background-color: #ffffff;
}

.container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 20px;
}

.section-title {
  font-size: 32px;
  font-weight: 600;
  color: #212529;
  margin-bottom: 30px;
  text-align: center;
}

.categories-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 24px;
}

.category-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24px;
  background-color: #f8f9fa;
  border-radius: 8px;
  transition: all 0.3s ease;
  text-decoration: none;
  color: #212529;
  border: 1px solid #e9ecef;
}

.category-item:hover {
  transform: translateY(-4px);
  background-color: #ffffff;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.08);
}

.category-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.category-name {
  font-size: 16px;
  font-weight: 500;
  transition: color 0.2s ease;
}

.category-item:hover .category-name {
  color: #6c757d;
}

/* 热销商品样式 */
.hot-products-section {
  margin: 60px 0;
  padding: 40px 0;
  background-color: #ffffff;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.view-more {
  font-size: 16px;
  color: #6c757d;
  text-decoration: none;
  transition: color 0.2s ease;
  font-weight: 500;
}

.view-more:hover {
  color: #212529;
}

.products-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 30px;
  max-width: 1400px;
  margin: 0 auto;
}

.product-card {
  background-color: #ffffff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  transition: all 0.3s ease;
  border: 1px solid #e9ecef;
}

.product-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.08);
}

.product-link {
  text-decoration: none;
  display: block;
}

.product-image-wrapper {
  position: relative;
  height: 300px;
  overflow: hidden;
  background-color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
}

.product-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
  transition: transform 0.5s ease;
}

.product-card:hover .product-image {
  transform: scale(1.03);
}

.discount-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  background-color: #212529;
  color: #ffffff;
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 600;
}

.product-info {
  padding: 20px;
  background-color: #ffffff;
}

.product-name {
  font-size: 17px;
  font-weight: 500;
  color: #212529;
  margin-bottom: 10px;
  height: 48px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  transition: color 0.2s ease;
}

.product-name:hover {
  color: #6c757d;
}

.product-rating {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 12px;
  font-size: 14px;
  color: #6c757d;
}

.rating-stars {
  color: #6c757d;
}

.rating-value {
  font-weight: 600;
  color: #212529;
}

.product-price {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}

.current-price {
  font-size: 24px;
  font-weight: 600;
  color: #212529;
}

.original-price {
  font-size: 16px;
  color: #adb5bd;
  text-decoration: line-through;
}

.product-sales {
  font-size: 14px;
  color: #6c757d;
}

/* 响应式设计 */
@media (max-width: 1440px) {
  .container {
    max-width: 1200px;
  }
}

@media (max-width: 1280px) {
  .container {
    max-width: 1100px;
  }
}

@media (max-width: 1024px) {
  .banner-carousel {
    height: 400px;
  }
  
  .banner-title {
    font-size: 36px;
  }
  
  .banner-description {
    font-size: 18px;
  }
  
  .categories-grid {
    grid-template-columns: repeat(4, 1fr);
  }
  
  .products-grid {
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  }
}

@media (max-width: 768px) {
  .container {
    padding: 0 15px;
  }
  
  .banner-carousel {
    height: 300px;
  }
  
  .banner-content {
    left: 5%;
    right: 5%;
  }
  
  .banner-title {
    font-size: 28px;
  }
  
  .banner-description {
    font-size: 16px;
  }
  
  .categories-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
  }
  
  .section-title {
    font-size: 24px;
  }
  
  .products-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
    padding: 0 15px;
  }
  
  .product-image-wrapper {
    height: 200px;
  }
}

@media (max-width: 480px) {
  .banner-carousel {
    height: 200px;
  }
  
  .banner-title {
    font-size: 20px;
    margin-bottom: 8px;
  }
  
  .banner-description {
    font-size: 14px;
    margin-bottom: 12px;
  }
  
  .categories-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .section-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  
  .products-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
  
  .product-image-wrapper {
    height: 240px;
  }
}
</style>
